package com.zenika.zenbinding.client.converter;

import com.zenika.zenbinding.client.exception.ConverterException;
import com.zenika.zenbinding.client.model.ZenEnum;

public class EnumConverter implements Converter {

	private ZenEnum[] enumValues;

	public EnumConverter(ZenEnum[] enumValues) {
		this.enumValues = enumValues;
	}

	public String fromObjectToString(Object object) throws ConverterException {
		for (int index = 0; index < enumValues.length; index++) {
			ZenEnum currentEnum = (ZenEnum) enumValues[index];
			if (currentEnum.getKey() != null && currentEnum.getKey().equals(object))
				return currentEnum.getValue();
		}
		return null;
	}

	public Object fromStringToObject(String string) throws ConverterException {
		for (int index = 0; index < enumValues.length; index++) {
			ZenEnum currentEnum = enumValues[index];
			if (currentEnum.getValue() != null && currentEnum.getValue().equals(string))
				return currentEnum.getKey();
		}
		return null;
	}

}
